package com.ruoyi.web.controller.systembasic;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SmesMaterialCategory;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.systembasic.service.ISmesMaterialCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 物料类别Controller
 *
 * @author simpsense
 * @date 2020-11-27
 */
@RestController
@RequestMapping("/systembasic/materialCategory")
public class SmesMaterialCategoryController extends BaseController
{
    @Autowired
    private ISmesMaterialCategoryService smesMaterialCategoryService;

    /**
     * 查询物料类别列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:materialCategory:list')")
    @GetMapping("/list")
    public AjaxResult list(SmesMaterialCategory smesMaterialCategory)
    {
        List<SmesMaterialCategory> list = smesMaterialCategoryService.selectSmesMaterialCategoryList(smesMaterialCategory);
        return AjaxResult.success(list);
    }


    /**
     * 获取产品类别下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SmesMaterialCategory smesMaterialCategory)
    {
        List<SmesMaterialCategory> list = smesMaterialCategoryService.selectSmesMaterialCategoryList(smesMaterialCategory);;
        return AjaxResult.success(smesMaterialCategoryService.buildMaterialCategoryTreeSelect(list));
    }
    /**
     * 导出物料类别列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:materialCategory:export')")
    @Log(title = "物料类别", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmesMaterialCategory smesMaterialCategory)
    {
        List<SmesMaterialCategory> list = smesMaterialCategoryService.selectSmesMaterialCategoryList(smesMaterialCategory);
        ExcelUtil<SmesMaterialCategory> util = new ExcelUtil<SmesMaterialCategory>(SmesMaterialCategory.class);
        return util.exportExcel(list, "materialCategory");
    }

    /**
     * 获取物料类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('systembasic:materialCategory:query')")
    @GetMapping(value = "/{materialCategoryId}")
    public AjaxResult getInfo(@PathVariable("materialCategoryId") Long materialCategoryId)
    {
        return AjaxResult.success(smesMaterialCategoryService.selectSmesMaterialCategoryById(materialCategoryId));
    }

    /**
     * 新增物料类别
     */
    @PreAuthorize("@ss.hasPermi('systembasic:materialCategory:add')")
    @Log(title = "物料类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmesMaterialCategory smesMaterialCategory)
    {
        smesMaterialCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(smesMaterialCategoryService.insertSmesMaterialCategory(smesMaterialCategory));
    }

    /**
     * 修改物料类别
     */
    @PreAuthorize("@ss.hasPermi('systembasic:materialCategory:edit')")
    @Log(title = "物料类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmesMaterialCategory smesMaterialCategory)
    {
        if(smesMaterialCategory.getParentId().equals(smesMaterialCategory.getMaterialCategoryId())){
            return AjaxResult.error("修改产品类别'" + smesMaterialCategory.getMaterialCategoryId() + "'失败，上级类别不能是自己");
        }
        smesMaterialCategory.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(smesMaterialCategoryService.updateSmesMaterialCategory(smesMaterialCategory));
    }

    /**
     * 删除物料类别
     */
    @PreAuthorize("@ss.hasPermi('systembasic:materialCategory:remove')")
    @Log(title = "物料类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialCategoryIds}")
    public AjaxResult remove(@PathVariable Long[] materialCategoryIds)
    {
        return toAjax(smesMaterialCategoryService.deleteSmesMaterialCategoryByIds(materialCategoryIds));
    }
}